<%--
  Created by IntelliJ IDEA.
  User: lzh
  Date: 2017/6/15
  Time: 18:07
  出库单编辑
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:include page="/_meta.jsp"/>
<title>详情</title>
<link href="/lib/easyui/themes/default/easyui.css" rel="stylesheet" type="text/css">
<link href="/lib/easyui/themes/icon.css" rel="stylesheet" type="text/css">
</head>
<body>
<article class="page-container">
    <form action="" method="post" class="form form-horizontal" id="form-detail">
        <div class="row cl">
            <label class="form-label col-xs-4 mylabel"><span class="c-red">*</span>销售单号</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" id="saleId" value="${param.salesId}" disabled="disabled">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 mylabel"><span class="c-red">*</span>经办人</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input id="empMsg" class="input-text" type="text">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 mylabel"><span class="c-red">*</span>出库原因</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" id="stockReason" value="">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 mylabel">详细信息</label>
            <div class="formControls col-xs-8 col-sm-9">
                <table id="detail_tb"></table>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>

<jsp:include page="/_footer.jsp"/>
<script src="/lib/easyui/jquery.easyui.min.js"></script>
<script src="/lib/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
    $(function () {
        var tb_data;
        $.post("GetAllEmpServlet", function (data) {
            var JempMsg = $.parseJSON(data);
            var comb = new Array;
            for (var i = 0; i < JempMsg.length; i++) {
                var j = {workId: JempMsg[i].workId, name: JempMsg[i].name};
                comb[i] = j;
            }
            comb[0].selected = true;
            $("#empMsg").combobox({
                data: comb,
                width: 200,
                valueField: 'workId',//select下拉框的的value字段名，经办人的工号
                textField: 'name',//下拉框选项值  经办人的姓名
                filter: combFilter,
                required: true
            });
        });
        $.post("/GetAllStorServlet", {ajax: "comb"}, function (data) {//异步请求仓库的信息
            var sto_data = parsesto_data(data);
            $.post("GetPendingSal", {salesId: "${param.salesId}"}, function (data) {
                tb_data = parsetb_Data(data);//数据转化为表格数据
                $("#detail_tb").datagrid({
                    data: tb_data,
                    singleSelect: true,
                    iconCls: 'icon-ok',
                    title: '详情',
                    rownumbers: true,
                    onClickCell: rowClicked,
                    columns: [[
                        {field: 'salesId', title: '编号'},
                        {field: 'proBarCode', title: '商品条码'},
                        {field: 'proName', title: '商品名'},
                        {field: 'saleQuantity', title: '数量'},
                        {field: 'salePrice', title: '单价'},
                        {field: 'saleDiscount', title: '折扣'},
                        {field: 'saleTotal', title: '总价'},
                        {
                            field: 'InvOutOfSto', title: '选择出货仓库', width: 150,
                            editor: {
                                type: 'combobox',
                                options: {
                                    //url:'/GetAllStorServlet',//URL加载不上数据
                                    data: sto_data,
                                    width: 120,
                                    valueField: 'id',//select下拉框的的value字段名 仓库id
                                    textField: 'text',//下拉框选项值 仓库名
                                    filter: combFilter,
                                    //onSelect: getInv,
                                }
                            }
                        },
                        {field: 'InvOfPro', title: '库存量'},
                        //{field: 'execute', title: '操作', formatter: form2, width: 60}
                    ]],
                    onLoadSuccess: function (data) {//data为后台返回的json
                        //alert(data.rows.length);
                        $("a.easyui-linkbutton").linkbutton();
                        for (var i = 0; i < data.rows.length; i++) {
                            $("#detail_tb").datagrid('beginEdit', i);//加载成功开启编辑器
                        }
                    }
                });
            });

        });


    });
    function form1(value, row, index) {
        return "<div class='formControls col-xs-8 col-sm-9'> <input class='form1 input-text' type='text'></div>";
    }
    function form2(value, row, index) {
        return "<a  onclick='form2Click(" + index + ")' class='easyui-linkbutton' iconCls='icon-redo'>出库</a>"
    }
    function parsetb_Data(data) {//解析服务器回传的数据到表格中
        var jdata = $.parseJSON(data);
        var jtb_date = {total: 1, rows: []};//表格需要数据的格式头
        for (var i = 0; i < jdata.length; i++) {
            jtb_date.rows[i] = {
                salesId: jdata[i].sale.salesId,
                proBarCode: jdata[i].product.proBarCode,
                proName: jdata[i].product.proName,
                saleQuantity: jdata[i].saleQuantity,
                salePrice: jdata[i].salePrice,
                saleDiscount: jdata[i].saleDiscount,
                saleTotal: jdata[i].saleTotal,
                InvOfPro: "点击获取"
            };
        }
        return jtb_date;
    }
    function parsesto_data(data) {//解析数据到combbox
        return $.parseJSON(data);
    }
    function combFilter(q, row) {
        var opts = $(this).combobox('options');
        return row[opts.textField].indexOf(q) >= 0;//带输入的下拉框本地检索
    }
    //    var combField={};
    //    function getInv(record) {//参数为当前的两个field
    //        combField=record;
    //    }
    var combData = {};
    function rowClicked(rowIndex,filed) {
        $("#detail_tb").datagrid("selectRow",rowIndex);
        var rowData=$("#detail_tb").datagrid("getSelected");
        if(filed=="InvOfPro"){
            var ed = $("#detail_tb").datagrid("getEditor", {index: rowIndex, field: "InvOutOfSto"});
            var combId = $(ed.target).val();
            if (combData.hasOwnProperty(rowData.proBarCode)) {//数据已经在服务器请求过
                if (combData[rowData.proBarCode][combId] >= rowData.saleQuantity) {
                    rowData.InvOfPro = combData[rowData.proBarCode][combId];
                }
                else {//显示红色字体
                    if(combData[rowData.proBarCode][combId]==undefined){
                        rowData.InvOfPro = "<span style='color: red'>0</span>";
                    }
                    else {
                        rowData.InvOfPro = "<span style='color: red'>" + combData[rowData.proBarCode][combId] + "</span>";
                    }

                }
                initRow(rowIndex);
                $("#detail_tb").datagrid("refreshRow",rowIndex);


            }
            else {//没有加载
                $.post("GetInvServlet", {proBarCode: rowData.proBarCode}, function (data) {
                    var jdata = $.parseJSON(data);
                    var sto = {};
                    for (var i = 0; i < jdata.length; i++) {
                        var stoId = jdata[i].storage.stoId;//仓库id
                        var num = jdata[i].realInvCount;//数量
                        sto[stoId] = num;
                    }
                    combData[rowData.proBarCode] = sto; //此商品的所有仓库的库存信息
                    if (combData[rowData.proBarCode][combId] >= rowData.saleQuantity) {
                        rowData.InvOfPro = combData[rowData.proBarCode][combId];
                    }
                    else {//显示红色字体
                        if(combData[rowData.proBarCode][combId]==undefined){
                            rowData.InvOfPro = "<span style='color: red'>0</span>";
                        }
                        else {
                            rowData.InvOfPro = "<span style='color: red'>" + combData[rowData.proBarCode][combId] + "</span>";
                        }
                    }
                    initRow(rowIndex);
                    $("#detail_tb").datagrid("refreshRow",rowIndex);

                });
            }
        }
        else if(filed=="InvOutOfSto"){
            rowData.InvOfPro="点击获取";
            $("#detail_tb").datagrid("refreshRow",rowIndex);
            $("#detail_tb").datagrid("beginEdit",rowIndex);
        }

    }
    function initRow(rowIndex) {
        //$("#detail_tb").datagrid("beginEdit",rowIndex);
        $("#detail_tb").datagrid("endEdit",rowIndex);
    }
    $(".btn").click(function () {
        var rows=$("#detail_tb").datagrid("getRows");
        for(var i=0;i<rows.length;i++){
            if(rows[i].InvOfPro.toString()=="点击获取"||rows[i].InvOfPro.toString().indexOf("<span")==0){
                layer.tips("商品"+rows[i].proBarCode+"在此仓库库存不够，无法提交",".btn");
                break;
            }
            else{//出库
                var outData={};//写入出库表需要的信息
                outData["saleId"]=$("#saleId").val();
                outData["empMsg"]=$("#empMsg").val();
                outData["stockReason"]=$("#stockReason").val();
                outData["detail"]=JSON.stringify(rows);
                $.post("OutStockServlet",outData,function (data) {
                    if(data=="true"){
                        layer.msg("提交成功,3s后此页面将自动关闭！");
                        setTimeout(function () {
                            parent.layer.close(1);
                        },3000);

                    }
                    else{
                        layer.msg("失败了！");
                    }
                });
            }
        }
    });
</script>
</body>
</html>